package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.pojo.Category;
import com.itheima.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Locale;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    //添加
    @PostMapping
    public R<String> save(@RequestBody Category category){
        categoryService.save(category);
        return R.success("添加成功");
    }

    //分页查询
    @GetMapping("/page")
    public  R<Page> page(int page, int pageSize){
        Page page1 = new Page(page,pageSize);

        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(Category::getSort);

        categoryService.page(page1,wrapper);
        return R.success(page1);
    }


    //修改
    @PutMapping
    public R<String> update(@RequestBody Category category){

        categoryService.updateById(category);
        return R.success("修改分类信息成功");
    }

    //删除
    @DeleteMapping
    public R<String> remove(Long id){
        categoryService.remove(id);
        return R.success("删除成功！");
    }


    //通过分类获取到添加菜品里的   —“菜品分类”—
   @GetMapping("/list")
    public R<List<Category>> list (Category category){
       LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
       wrapper.eq(category.getType()!=null,Category::getType,category.getType());
       wrapper.orderByAsc(Category::getSort).orderByAsc(Category::getCreateTime);

       List<Category> list = categoryService.list(wrapper);
       return R.success(list);
   }









}
